ident = '$Id: contacts.py 116 2006-06-16 17:02:14Z trevor $'

'''The Contact and Instance UI components'''
import sys, wx, rex, common, images, re, base
from xml.dom		import minidom

ID_CNT_SAVE			= 100
ID_CNT_DELETE		= 101


BY_ALL				= 0
BY_FN				= 1
BY_LN				= 2
BY_ID				= 3
BY_XID				= 4
BY_EMAIL			= 5
BY_COMPANY			= 6
BY_USERNAME			= 7
BY_PASSWORD			= 8
BY_GID				= 9
BY_SPID				= 10
BY_BPID				= 11

BY_GROUP_ALL		= 0
BY_GROUP_NAME		= 1
BY_GROUP_ID			= 2
BY_GROUP_SPID		= 3
BY_GROUP_BPID		= 4

ID_INSTANCE_APPLY	= 20
ID_INSTANCE_UNAPPLY	= 21

class contact( base.editor ):
	def __init__( self, account, con, listindex ):
		base.editor.__init__( self, account, con, listindex )
		self.load_rex_object()

	def load_rex_object( self ):
		if self.rex_object is None: return
		# Instances need to be loaded from rex
		if self.account.window: self.account.window.SetStatusText("Loading contact details..")
		wx.Yield()
		cur = wx.BusyCursor()
		xmlstring = ''
		try:
			xmlstring = self.account.execute( 'base', "contact_details_get2", ID=self.rex_object["id"] )
			self.rex_object.fromxml( xmlstring )
		except:
			common.show_error( "failed to load the contact" )
		del cur

		xmlstring = common.prepare_xml( xmlstring )
		xmlstring = xmlstring.encode('utf-8')
		sdom 	  = minidom.parseString( xmlstring )
		snode	  = sdom.firstChild
		ilist     = self.subscription_editor.list
		for inode in snode.getElementsByTagName("subscribed"):
			name	= inode.attributes["name"].value
			pid 	= inode.attributes["id"].value
			module 	= inode.attributes["module"].value
			icon = self.subscription_editor.folder_w_icon
			if module.lower() == 'newsletter':
				icon = self.subscription_editor.folder_n_icon
			elif module.lower() == 'email':
				icon = self.subscription_editor.folder_e_icon
			gname	= inode.attributes["group_name"].value
			gid		= inode.attributes["group_id"].value
			# for now, just use the web
			x = ilist.InsertImageStringItem( sys.maxint, pid, icon )
			# The publication name
			ilist.SetStringItem   ( x , 1, name  )
			# Through group id
			ilist.SetStringItem   ( x , 2, gid, self.block_editor.group_icon )
			# Through group name
			ilist.SetStringItem   ( x , 3, gname  )
		self.subscription_editor.resize_columns()

		ilist     = self.block_editor.list
		for inode in snode.getElementsByTagName("blocked"):
			name	= inode.attributes["name"].value
			pid		= inode.attributes["id"].value
			module 	= inode.attributes["module"].value
			icon = self.block_editor.folder_w_icon
			if module.lower() == 'newsletter':
				icon = self.block_editor.folder_n_icon
			elif module.lower() == 'email':
				icon = self.block_editor.folder_e_icon
			gname	= inode.attributes["group_name"].value
			gid		= inode.attributes["group_id"].value
			# for now, just use the web
			x = ilist.InsertImageStringItem( sys.maxint, pid, icon )
			# The publication name
			ilist.SetStringItem   ( x , 1, name  )
			# Through group id
			ilist.SetStringItem   ( x , 2, gid, self.block_editor.group_icon  )
			# Through group name
			ilist.SetStringItem   ( x , 3, gname  )
		self.block_editor.resize_columns()

		ilist     = self.application_editor.list
		for inode in snode.getElementsByTagName("instance"):
			id		= "%s.%s" % ( inode.attributes["binder_id"].value, inode.attributes["instance_id"].value )
			name	= inode.attributes["instance_name"].value
			gid		= inode.attributes["group_id"].value
			gname	= inode.attributes["group_name"].value
			x = ilist.InsertImageStringItem( sys.maxint, id, self.application_editor.instance_icon )
			ilist.SetStringItem   ( x, 1, name)
			item = ilist.GetItem( x )
			if int(gid) == 0:
				ilist.SetStringItem   ( x, 2, '')
				ilist.SetStringItem   ( x, 3, 'Applied directly')
			else:
				ilist.SetItemData	  ( x, int( gid ) )
				ilist.SetItemBackgroundColour( x, common.sui_colors("whitesmoke") )
				#ilist.SetItemImage ( x, 1, self.application_editor.group_icon )
				ilist.SetStringItem   ( x, 2, gid, self.application_editor.group_icon )
				ilist.SetStringItem   ( x, 3, gname)
		self.application_editor.resize_columns()

		# Add the standard properties
		base.editor.load_rex_object( self )
		# Reposition the splitter
		if self.account.window: self.account.window.SetStatusText( "Ready" )

class group( base.editor ):
	def __init__( self, account, grp, listindex ):
		base.editor.__init__( self, account, grp, listindex )
		self.list_item = None
		self.load_rex_object()

	def load_rex_object( self ):
		if self.rex_object is None: return
		# Instances need to be loaded from rex
		# Issues need to be loaded from eas
		if self.account.window: self.account.window.SetStatusText("Loading group..")
		wx.Yield()
		cur = wx.BusyCursor()
		xmlstring = ''
		try:
			xmlstring = self.account.execute( 'base', "group_details_get2", ID=self.rex_object["id"] )
		except:
			common.show_error( "failed to load the group" )
		del cur
		xmlstring = common.prepare_xml( xmlstring )
		xmlstring = xmlstring.encode('utf-8')
		sdom 	  = minidom.parseString( xmlstring )
		snode	  = sdom.firstChild
		ilist     = self.subscription_editor.list
		for inode in snode.getElementsByTagName("subscribed"):
			name = inode.attributes["name"].value
			pid  = inode.attributes["id"].value
			# for now, just use the web
			x = ilist.InsertImageStringItem( sys.maxint, pid, self.subscription_editor.folder_w_icon )
			ilist.SetStringItem   ( x , 1, name  )
		self.subscription_editor.resize_columns()

		ilist     = self.block_editor.list
		for inode in snode.getElementsByTagName("blocked"):
			name = inode.attributes["name"].value
			pid  = inode.attributes["id"].value
			# for now, just use the web
			x = ilist.InsertImageStringItem( sys.maxint, pid, self.block_editor.folder_w_icon )
			ilist.SetStringItem   ( x , 1, name  )
		self.subscription_editor.resize_columns()

		ilist     = self.application_editor.list
		for inode in snode.getElementsByTagName("instance"):
			name		= inode.attributes["instance_name"].value
			binder_id	= inode.attributes["binder_id"].value
			instance_id	= inode.attributes["instance_id"].value
			x = ilist.InsertImageStringItem( sys.maxint, "%s.%s" % ( binder_id, instance_id ), self.application_editor.instance_icon )
			ilist.SetStringItem   ( x , 1, name )
		self.application_editor.resize_columns()

		base.editor.load_rex_object( self )
		# Reposition the splitter
		if self.account.window: self.account.window.SetStatusText( "Ready" )
		
class request( base.editor ):
	def __init__( self, account, req, listindex ):
		base.editor.__init__( self, account, req, listindex )
		self.load_rex_object()

	def load_rex_object( self ):
		if self.rex_object is None: return
		# Instances need to be loaded from rex
		if self.account.window: self.account.window.SetStatusText("Loading request details..")
		wx.Yield()
		cur = wx.BusyCursor()
		xmlstring = ''
		try:
			xmlstring = self.account.execute( 'base', "request_details_get", ID=self.rex_object["id"] )
			self.rex_object.fromxml( xmlstring )
		except:
			common.show_error( "failed to load the contact" )
		del cur

		xmlstring = common.prepare_xml( xmlstring )
		xmlstring = xmlstring.encode('utf-8')
		sdom 	  = minidom.parseString( xmlstring )
		snode	  = sdom.firstChild

		# Add the standard properties
		base.editor.load_rex_object( self )
		# Reposition the splitter
		if self.account.window: self.account.window.SetStatusText( "Ready" )
